package com.ctgu.demo.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctgu.demo.entity.*;
import com.ctgu.demo.error.EmBusinessError;
import com.ctgu.demo.error.exception.BusinessException;
import com.ctgu.demo.error.response.CommonReturnType;
import com.ctgu.demo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * @program: AdminController
 * @description:
 * @author: gh
 * @create: 2020-06-22 12:24
 **/
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
@RestController
@RequestMapping("/admin")
public class AdminController {

    private static final String CONTENT_TYPE_FORMED ="application/x-www-form-urlencoded" ;
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;
    @Autowired
    private DessertService dessertService;
    @Autowired
    private CakeService cakeService;
    @Autowired
    private PuddingService puddingService;
    @Autowired
    private HttpServletRequest httpServletRequest;

    @RequestMapping("/getUser/{page}/{size}")
    public IPage<User> getUser(@PathVariable("page")int page, @PathVariable("size")int size){
        return userService.getAll(page,size);
    }

    @RequestMapping("/getDessert/{page}/{size}")
    public IPage<Dessert> getDessert(@PathVariable("page")int page, @PathVariable("size")int size){
        return dessertService.getAll(page,size);
    }

    @RequestMapping("/getCake/{page}/{size}")
    public IPage<Cake> getCake(@PathVariable("page")int page, @PathVariable("size")int size){
        return cakeService.getAll(page,size);
    }

    @RequestMapping("/getPudding/{page}/{size}")
    public IPage<Pudding> getPudding(@PathVariable("page")int page, @PathVariable("size")int size){
        return puddingService.getAll(page,size);
    }

    //增
    @PostMapping("/addUser")
    public int addUser(@RequestBody User user) {
        System.out.println(user);
        userService.save(user);
        return 1;
    }

    @PostMapping("/addDessert")
    public int addDessert(@RequestBody Dessert dessert) {
        System.out.println(dessert);
        dessertService.save(dessert);
        return 1;
    }

    @PostMapping("/addCake")
    public int addCake(@RequestBody Cake cake) {
        System.out.println(cake);
        cakeService.save(cake);
        return 1;
    }

    @PostMapping("/addPudding")
    public int addPudding(@RequestBody Pudding pudding) {
        System.out.println(pudding);
        puddingService.save(pudding);
        return 1;
    }

    //删
    @GetMapping("/deleteUser/{id}")
    public int deleteUser(@PathVariable("id") String id) {
        System.out.println(id);
        userService.removeById(id);
        return 1;
    }

    @GetMapping("/deleteDessert/{dessertId}")
    public int deleteDessert(@PathVariable("dessertId") String dessertId) {
        System.out.println(dessertId);
        dessertService.removeById(dessertId);
        return 1;
    }

    @GetMapping("/deleteCake/{cakeId}")
    public int deleteCake(@PathVariable("cakeId") int cakeId) {
        System.out.println(cakeId);
        cakeService.removeById(cakeId);
        return 1;
    }

    @GetMapping("/deletePudding/{puddingId}")
    public int deletePudding(@PathVariable("puddingId") int puddingId) {
        System.out.println(puddingId);
        puddingService.removeById(puddingId);
        return 1;
    }

    //改
    @RequestMapping("/getUserOne/{id}")
    public User getUser(@PathVariable("id") String id){
        return userService.getById(id);
    }
    @PostMapping("/updateUser")
    public int updateUser(@RequestBody User user) {
        System.out.println(user);
        userService.updateById(user);
        return 1;
    }

    @RequestMapping("/getDessertOne/{dessertId}")
    public Dessert getDessert(@PathVariable("dessertId") String dessertId){
        return dessertService.getById(dessertId);
    }
    @PostMapping("/updateDessert")
    public int updateDessert(@RequestBody Dessert dessert) {
        System.out.println(dessert);
        dessertService.updateById(dessert);
        return 1;
    }

    @RequestMapping("/getCakeOne/{cakeId}")
    public Cake getCake(@PathVariable("cakeId") String cakeId){
        return cakeService.getById(cakeId);
    }
    @PostMapping("/updateCake")
    public int updateCake(@RequestBody Cake cake) {
        System.out.println(cake);
        cakeService.updateById(cake);
        return 1;
    }

    @RequestMapping("/getPuddingOne/{puddingId}")
    public Pudding getPudding(@PathVariable("puddingId") String puddingId){
        return puddingService.getById(puddingId);
    }
    @PostMapping("/updatePudding")
    public int updatePudding(@RequestBody Pudding pudding) {
        System.out.println(pudding);
        puddingService.updateById(pudding);
        return 1;
    }

    //查
    @GetMapping("/selectDessert/{name}/{page}/{size}")
    public IPage<Dessert> selectDessert(@PathVariable("name")String name,
                                     @PathVariable("page")int page,
                                     @PathVariable("size")int size){
        return dessertService.selectDessert(name, page, size);
    }

    @GetMapping("/selectCake/{name}/{page}/{size}")
    public IPage<Cake> selectCake(@PathVariable("name")String name, @PathVariable("page")int page, @PathVariable("size")int size){
        return cakeService.selectCake(name, page, size);
    }

    @GetMapping("/selectPudding/{name}/{page}/{size}")
    public IPage<Pudding> selectPudding(@PathVariable("name")String name,
                                  @PathVariable("page")int page,
                                  @PathVariable("size")int size){
        return puddingService.selectPudding(name, page, size);
    }

    @GetMapping("/selectUser/{name}/{page}/{size}")
    public IPage<User> selectUser(@PathVariable("name")String name,
                                    @PathVariable("page")int page,
                                    @PathVariable("size")int size){
        return userService.selectUser(name, page, size);
    }

    @RequestMapping(value = "/login",method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
    public CommonReturnType login(@RequestParam(name = "telephone")String adminTelephone,
                                  @RequestParam(name = "password")String password) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
        //入参校验
        if (org.apache.commons.lang3.StringUtils.isEmpty(adminTelephone) ||
                StringUtils.isEmpty(password)) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        //校验登陆是否合法
        Admin admin = adminService.validateLogin(adminTelephone,this.encodeByMd5(password));

        //将登陆凭证加入到用户登陆成功的session内
        this.httpServletRequest.getSession().setAttribute("IS_LOGIN",true);
        this.httpServletRequest.getSession().setAttribute("LOGIN_ADMIN",admin);
        return CommonReturnType.create(null);
    }
    public String encodeByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String newstr = base64Encoder.encode(md5.digest(str.getBytes("utf-8")));
        return newstr;
    }

}
